﻿///<summary>
/// 说明：上传文件类（需要改善）
/// 上传文件并生成缩略图，还可以打水印
/// 创建：邱思良
/// 时间：2008-03-25
/// 引用：
/// if (IsPostBack)
/// {
///     UploadIMG imgup = new UploadIMG();
///     imgup.Path = "files\\uploadimg";
///     if (imgup.UpLoadIMG(FileUpload1))
///     { }
///     else
///     {
///     Response.Write("上传出现一点点问题哦:" + imgup.MSG.ToString());
/// }   
/// 
/// <asp:FileUpload ID="FileUpload1" runat="server" 
/// <asp:Button ID="Button1" runat="server" Text="上传"  />
///</summary>


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Drawing;
using System.IO;
using System.Web.UI.WebControls;

namespace Wozuji.Common.Image
{

    /// <summary>
    /// UploadIMG 的摘要说明
    /// </summary>
    public class UpLoadImage
    {
        public UpLoadImage()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
            //UpLoadIMG（this.fileupload1）
            //
        }
        private string _MSG;
        private string _ofullname;
        private string _tfullname;
        private string _ofilename;
        private string _tfilename;

        private int _limitwidth = 2048;
        private int _limitheight = 1536;

        private int _twidth = 200;
        private int _theight = 200;

        private int _size = 10000000;
        private bool _israte = true;

        private string _path = "UpdateFile";
        private Font _ofont = new Font("宋体", 10);
        private Brush _obrush = new SolidBrush(Color.Black);

        /// <summary>
        /// 信息
        /// </summary>
        public string MSG
        {
            get { return _MSG; }
            set { _MSG = value; }
        }

        /// <summary>
        /// 保存时的完整路径.原图
        /// </summary>
        public string OFullName
        {
            get { return _ofullname; }
            set { _ofullname = value; }
        }

        /// <summary>
        ///打水印文字字体 
        /// </summary>
        public Font Ofont
        {
            get { return Ofont; }
            set { _ofont = value; }
        }



        /// <summary>
        /// 保存时的完整路径.缩略图
        /// </summary>
        public string TFullName
        {
            get { return _tfullname; }
            set { _tfullname = value; }
        }

        /// <summary>
        /// 保存时的图片名称.原图
        /// </summary>
        public string OFileName
        {
            get { return _ofilename; }
            set { _ofilename = value; }
        }

        /// <summary>
        /// 保存时的图片名称.缩略图
        /// </summary>
        public string TFileName
        {
            get { return _tfilename; }
            set { _tfilename = value; }
        }

        /// <summary>
        /// 限定宽度
        /// </summary>
        public int LimitWidth
        {
            get { return _limitwidth; }
            set { _limitwidth = value; }
        }

        /// <summary>
        /// 限定高度
        /// </summary>
        public int LimitHeight
        {
            get { return _limitheight; }
            set { _limitheight = value; }
        }

        /// <summary>
        /// 缩略图宽度
        /// </summary>
        public int TWidth
        {
            get { return _twidth; }
            set { _twidth = value; }
        }

        /// <summary>
        /// 缩略图高度
        /// </summary>
        public int THeight
        {
            get { return _theight; }
            set { _theight = value; }
        }

        /// <summary>
        /// 文件大小
        /// </summary>
        public int Size
        {
            get { return _size; }
            set { _size = value; }
        }

        /// <summary>
        /// 是否成比例
        /// </summary>
        public bool IsRate
        {
            get { return _israte; }
            set { _israte = value; }
        }

        /// <summary>
        /// 存放图片的文件夹名称
        /// </summary>
        public string Path
        {
            get { return _path; }
            set { _path = value; }
        }

        /// <summary>
        /// 图片上传(默认:"等比压缩,限定上传尺寸2048*1536,缩略图尺寸100*100,限定上传大小1MB,存放在根目录UpdateFile中")
        /// </summary>
        /// <param name="UploadFile">文件上传控件</param>
        /// <returns>返回是否成功保存图片</returns>
        public bool UpLoadIMG(FileUpload UploadFile)
        {
            if (UploadFile.HasFile)//检查是否已经选择文件
            {

                string filename = UploadFile.FileName.ToLower();
                int i = filename.LastIndexOf(".");
                filename = filename.Substring(i, filename.Length - i);

                if (!(filename == ".jpg" || filename == ".jpeg" || filename == ".gif" || filename == ".png" || filename == ".bmp"))
                {
                    MSG = "不受支持的类型,请重新选择!";
                    return false;
                }//检查上传文件的格式是否有效


                if (UploadFile.PostedFile.ContentLength == 0 || UploadFile.PostedFile.ContentLength >= Size)
                {
                    MSG = "指定的文件大小不符合要求!" + UploadFile.PostedFile.ContentLength.ToString();
                    return false;
                }//检查图片文件的大小

                //生成原图 
                Stream oStream = UploadFile.PostedFile.InputStream;
                System.Drawing.Image oImage = System.Drawing.Image.FromStream(oStream);
                System.Drawing.Font font = new Font("宋体", 10);
                System.Drawing.Brush brush = new SolidBrush(Color.Black);

                //打文字水印
                //Graphics o = Graphics.FromImage(oImage);
                //o.DrawString("3every.com", font, brush, owidth - 70, oheight - 20);

                int owidth = oImage.Width; //原图宽度 
                int oheight = oImage.Height; //原图高度

                if (owidth > LimitWidth || oheight > LimitHeight)
                {
                    MSG = "超过允许的图片尺寸范围!";
                    return false;
                }//检查是否超出规定尺寸

                if (IsRate)
                {
                    //按比例计算出缩略图的宽度和高度 
                    if (owidth >= oheight)
                    {
                        THeight = (int)Math.Floor(Convert.ToDouble(oheight) * (Convert.ToDouble(TWidth) / Convert.ToDouble(owidth)));//等比设定高度
                    }
                    else
                    {
                        TWidth = (int)Math.Floor(Convert.ToDouble(owidth) * (Convert.ToDouble(THeight) / Convert.ToDouble(oheight)));//等比设定宽度
                    }
                }

                //生成缩略原图 



                Bitmap tImage = new Bitmap(TWidth, THeight);

                Graphics g = Graphics.FromImage(tImage);
                //打图片水印
                Graphics o = Graphics.FromImage(oImage);
                System.Drawing.Image copyImage = System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Server.MapPath("files\\img\\log.gif"));
                //Graphics a = Graphics.FromImage(bitmap);
                o.DrawImage(copyImage, new Rectangle(oImage.Width - copyImage.Width, oImage.Height - copyImage.Height, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel);





                //打文字水印
                //o.DrawImage("3every.com", new Font("宋体", 16), brush, owidth - 70, oheight - 20);



                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量插值法 
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//设置高质量,低速度呈现平滑程度 
                g.Clear(Color.Transparent); //清空画布并以透明背景色填充 
                g.DrawImage(oImage, new Rectangle(0, 0, TWidth, THeight), new Rectangle(0, 0, owidth, oheight), GraphicsUnit.Pixel);
                //打缩略图水印
                g.DrawString("3every.com", font, brush, TWidth - 70, THeight - 20);


                Random oRandom = new Random();
                string oStringRandom = oRandom.Next(9999).ToString();//生成4位随机数字

                //格式化日期作为文件名
                DateTime oDateTime = new DateTime();
                oDateTime = DateTime.Now;
                string oStringTime = oDateTime.ToString();
                oStringTime = oStringTime.Replace("-", "");
                oStringTime = oStringTime.Replace(" ", "");
                oStringTime = oStringTime.Replace(":", "");

                OFileName = "o" + oStringTime + oStringRandom + filename;
                TFileName = "t" + oStringTime + oStringRandom + filename;

                string oSavePath = HttpContext.Current.Server.MapPath("~") + "\\" + Path + "\\";
                if (!Directory.Exists(oSavePath))
                {
                    Directory.CreateDirectory(oSavePath);//在根目录下建立文件夹
                }

                //保存路径+完整文件名
                OFullName = oSavePath + OFileName;
                TFullName = oSavePath + TFileName;

                //开始保存图片至服务器
                try
                {
                    switch (filename)
                    {
                        case ".jpeg":
                        case ".jpg":
                            {

                                oImage.Save(OFullName, System.Drawing.Imaging.ImageFormat.Jpeg);
                                tImage.Save(TFullName, System.Drawing.Imaging.ImageFormat.Jpeg);
                                break;
                            }

                        case ".gif":
                            {
                                oImage.Save(OFullName, System.Drawing.Imaging.ImageFormat.Gif);
                                tImage.Save(TFullName, System.Drawing.Imaging.ImageFormat.Gif);
                                break;
                            }

                        case ".png":
                            {
                                oImage.Save(OFullName, System.Drawing.Imaging.ImageFormat.Png);
                                tImage.Save(TFullName, System.Drawing.Imaging.ImageFormat.Png);
                                break;
                            }

                        case ".bmp":
                            {
                                oImage.Save(OFullName, System.Drawing.Imaging.ImageFormat.Bmp);
                                tImage.Save(TFullName, System.Drawing.Imaging.ImageFormat.Bmp);
                                break;
                            }
                    }
                    MSG = "图片上传成功!";
                    return true;
                }
                catch (Exception ex)
                {
                    MSG = ex.Message;
                    return false;
                }
                finally
                {
                    //释放资源 
                    oImage.Dispose();
                    g.Dispose();
                    tImage.Dispose();
                }
            }
            else
            {
                MSG = "请先选择需要上传的图片!";
                return false;
            }
        }
    }
}

